package com.wenhui.schoolyardinfo.square.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.callback.RcvOnItemViewClickListener;
import com.wenhui.schoolyardinfo.manager.DateManager;
import com.wenhui.schoolyardinfo.square.bean.PublicGoodBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author    : mahongiyn
 * e-mail    : 1976222027@qq.com
 * date      : 2019/7/16 16:00
 * introduce : 我参加的公益Adapter
 */

public class MineJoinPublicGoodAdapter extends RecyclerView.Adapter<MineJoinPublicGoodAdapter.ContentHolder> {

    private static final String TAG = "MineJoinPublicGoodAdapter";
    private Context context;
    private List<PublicGoodBean> publicGoodBeanList = new ArrayList<>();

    public MineJoinPublicGoodAdapter(Context context) {
        this.context = context;
    }

    public MineJoinPublicGoodAdapter(Context context, List<PublicGoodBean> publicGoodBeanList) {
        this.context = context;
        this.publicGoodBeanList = publicGoodBeanList;
    }

    public void clearData() {
        publicGoodBeanList.clear();
        notifyDataSetChanged();
    }

    public void addAllData( List<PublicGoodBean> publicGoodBeanList) {

        this.publicGoodBeanList.addAll(publicGoodBeanList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ContentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        view = LayoutInflater.from(context).inflate(R.layout.item_mine_join_public_good_content, parent, false);
        return new ContentHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ContentHolder holder, int position) {
            ContentHolder contentHolder = (ContentHolder) holder;
            //加载宣传图片
            Glide.with(context).load(publicGoodBeanList.get(position ).getPublicGoodUrl()).into(contentHolder.imvPublicityPicture);
            contentHolder.tevTitle.setText(publicGoodBeanList.get(position ).getTitle());
            contentHolder.tevInitiator.setText(publicGoodBeanList.get(position ).getInitiator());

            String startTime[] = DateManager.millisecondConvertedToDate(publicGoodBeanList.get(position ).getStartTime()).split("-");
            String endTime[] = DateManager.millisecondConvertedToDate(publicGoodBeanList.get(position ).getEndTime()).split("-");
            contentHolder.tevTime.setText("时间：" +
                    startTime[0] + "." + startTime[1] + "." + startTime[2] + "-"
                    + endTime[0] + "." + endTime[1] + "." + endTime[2]);
            ;
    }


    @Override
    public int getItemCount() {
        return publicGoodBeanList.size();
    }

    protected class ContentHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @BindView(R.id.imv_publicity_picture)
        ImageView imvPublicityPicture;
        @BindView(R.id.tev_title)
        TextView tevTitle;
        @BindView(R.id.tev_initiator)
        TextView tevInitiator;
        @BindView(R.id.tev_time)
        TextView tevTime;
        @BindView(R.id.imv_partake)
        ImageView imvPartake;
        @BindView(R.id.tev_partake)
        TextView tevPartake;
        @BindView(R.id.layout_public_good)
        LinearLayout layoutPublicGood;

        private ContentHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            layoutPublicGood.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layout_public_good:
                    rcvOnItemViewClickListener.onItemClickListener(getAdapterPosition(), view);
                    break;
                default:
                    break;
            }
        }
    }

    private RcvOnItemViewClickListener rcvOnItemViewClickListener;

    public void setRcvOnItemViewClickListener(RcvOnItemViewClickListener rcvOnItemViewClickListener) {
        this.rcvOnItemViewClickListener = rcvOnItemViewClickListener;
    }
}
